/**
 * 
 */
package com.tianqi.rms.controller;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.tianqi.rms.model.business.Pics;
import com.tianqi.rms.model.business.Worker;
import com.tianqi.rms.util.OauthUtils;

/**
 *
 *                #####################################################
 *                #                                                   #
 *                #                       _oo0oo_                     #
 *                #                      o8888888o                    #
 *                #                      88" . "88                    #
 *                #                      (| -_- |)                    #
 *                #                      0\  =  /0                    #
 *                #                    ___/`---'\___                  #
 *                #                  .' \\|     |# '.                 #
 *                #                 / \\|||  :  |||# \                #
 *                #                / _||||| -:- |||||- \              #
 *                #               |   | \\\  -  #/ |   |              #
 *                #               | \_|  ''\---/''  |_/ |             #
 *                #               \  .-\__  '-'  ___/-. /             #
 *                #             ___'. .'  /--.--\  `. .'___           #
 *                #          ."" '<  `.___\_<|>_/___.' >' "".         #
 *                #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 *                #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 *                #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 *                #                       `=---='                     #
 *                #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 *                #                                                   #
 *                #               佛祖保佑         永无BUG              #
 *                #                        						      #
 *                #####################################################
 *								yanglong : Dec 11, 2018 6:45:40 PM 尊上
 */
public class indexController extends Controller {
	String base_url = PropKit.use("a_little_config.txt").get("baseUrl")+"index/";
	/**
	 *******************************************
	 * 方法名称:入口
	 * 创建人：yanglong
	 * 创建时间：2018年4月17日 下午11:44:27
	 * @return:void
	 *******************************************
	 */
	public void oauth() {
		String redirect_uri = base_url+"toIndex";
		String return_url = OauthUtils.publicOauth(redirect_uri);
		redirect(return_url);
	}
	
	
	public void toIndex() {
		String code = getPara("code");
		if(code != null){
			ApiConfig ac = ApiConfigKit.getApiConfig();
			ApiConfigKit.putApiConfig(ac);
			ApiConfigKit.setThreadLocalAppId(ac.getAppId());
			String appId = ac.getAppId();
			String secret = ac.getAppSecret();
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appId, secret, code);
			Integer errorCode=snsAccessToken.getErrorCode();
			if(errorCode!=null) {
				redirect("/index/oauth");
				return;
			}
			try {
				List<Worker> workers = Worker.dao.workers();
				for (Worker worker : workers) {
					worker.setPic(Pics.dao.findById(worker.getPhoto()));
				}
				setAttr("workers", workers);
				setAttr("pagetype", "index");
				render("index.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
